home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / ccdsrc.zip / SPEC.C < prev   
Text File  |  1991-09-01  |  3KB  |  130 lines

  1. /* Remove speckles from uosat pics 
  2.  
  3. 31JUL91 NK6K
  4.  
  5. Use after pacout and inter
  6.  
  7. Get a line.  See if a pixel is more than 20 different from its neighbors.
  8. If so, make it the average of its neighbors.
  9.  
  10. Also remove horizontal bar.
  11.  
  12. NOTE:  This algorithm not very good, but serves to make the images
  13. a little better.
  14.  
  15.  
  16. */
  17. #include <stdio.h>
  18. #include <dos.h>
  19. #include <fcntl.h>
  20. #include <sys\types.h>
  21. #include <sys\stat.h>
  22.  
  23. #define LINE_SIZE 612
  24. #define NUM_LINE_FIELD 288
  25.  
  26.  
  27. long start_field_2;
  28. int lines=0;
  29. int skip_lines=0;
  30. int correct;
  31. int correct_fudge=0;
  32. long doubles=0;
  33. unsigned char buff[LINE_SIZE+1];
  34. int fillval=0;
  35. long fixed=0;
  36. int correct_array[10] = {0,5,5,5,5,5,5,5,5,5};
  37.  
  38. main(argc,argv)
  39. int argc;
  40. char *argv[];
  41. {
  42.  
  43.     int fi1,fi2,fo;
  44.     int i;
  45.  
  46.  
  47.     if (argc<2) {
  48.         printf ("usage: spec input output val\n");
  49.         exit(1);
  50.     }
  51.  
  52.  
  53.     if ((fi1= open(argv[1], O_BINARY)) == -1){
  54.         printf("cannot open: %s\n",argv[1]);
  55.         perror("On input file");
  56.         exit(1);
  57.     }
  58.  
  59.  
  60.  
  61.     if ((fo = open(argv[2], 0)) != -1){
  62.         printf("output file exists.  Aborted\n");
  63.         exit(1);
  64.     }
  65.  
  66.     if ((fo = open(argv[2],O_CREAT | O_TRUNC | O_BINARY | O_RDWR, S_IREAD | S_IWRITE)) == -1) {
  67.         printf("cannot open: %s\n",argv[2]);
  68.         perror("On output file");
  69.         exit(1);
  70.     }
  71.  
  72.     
  73.     while (1) {        /* Do till there isn't any more */
  74.  
  75.     
  76.  
  77.         if (read(fi1, buff, LINE_SIZE)<LINE_SIZE) {
  78.             break;
  79.         }
  80.  
  81.         for (i=45;i<LINE_SIZE-1;i++) {
  82.             if ( (abs((int)buff[i]-(int)buff[i-1])>20) &&
  83.                  (abs((int)buff[i]-(int)buff[i+1])>20)) {
  84.                 /*printf("line %u:%u (%x %x %x)\n",lines,i,
  85.                     buff[i-1],buff[i],buff[i+1]);*/
  86.               /* check to see if the next pixel isn't a better
  87.                  candidate */
  88.               if ( ( abs((int)buff[i+1]-(int)buff[i-1]) ) >
  89.                  ( abs((int)buff[i]-(int)buff[i-1]))) {
  90.                 doubles++;
  91.               }
  92.               else {
  93.             
  94.                 fixed++;
  95.                 buff[i] = ((unsigned int)buff[i-1]+(unsigned int)buff[i+1])/2;
  96.     
  97.               }
  98.             }
  99.         }
  100.             
  101.         if (buff[34]!=0) {
  102.             correct=buff[34];
  103.             for (i=0;i<10;i++) correct+=buff[34+i];
  104.             correct/=10;
  105.             correct+=correct_array[correct_fudge];
  106.             correct_fudge++;
  107.             for (i=34;i<LINE_SIZE-1;i++) {
  108.                 if ( ((int) buff[i] - (int) correct)>0)
  109.                     buff[i] = (int) buff[i] - (int) correct    ;
  110.                 else buff[i]=0;
  111.             }
  112.         }
  113.  
  114.         if (write(fo,buff,LINE_SIZE)==-1) {
  115.             perror("On output file\n");
  116.             exit(1);
  117.         }
  118.  
  119. /*        printf("Line %u fixed %u  \r",lines,fixed);*/
  120.         lines++;
  121.  
  122.     }
  123.     printf("Fixed %lu, doubles %lu\n",fixed,doubles);
  124.     close (fo);
  125.     close (fi1);
  126.  
  127. }
  128.  
  129.  
  130.